package sample

import (
	"gorm.io/gorm"
	"organoid_public/domain/sample/model"
	"organoid_public/domain/sample/repository"
	"organoid_public/infrastructure/db"
)

type LoginAuth struct {
	db *gorm.DB
}

func NewLoginAuthRepository(db db.DefaultDb) repository.LoginAuth {
	return &LoginAuth{
		db: db,
	}
}

func (l *LoginAuth) FindAuth(tokenId string) bool {
	var loginAuth model.LoginAuth
	err := l.db.Debug().Model(&loginAuth).Where("token_id = ?", tokenId).Find(&loginAuth).Error
	return err == nil && loginAuth.Id > 0
}

func (l *LoginAuth) AddAuth(tokenId string) bool {
	var loginAuth model.LoginAuth
	loginAuth.TokenId = tokenId
	err := l.db.Debug().Model(&loginAuth).Create(&loginAuth).Error
	return err == nil

}

func (l *LoginAuth) DelAuth(tokenId string) bool {
	var loginAuth model.LoginAuth
	err := l.db.Debug().Model(&loginAuth).Where("token_id = ?", tokenId).Delete(&loginAuth).Error
	return err == nil
}
